// pages/gift/gift.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_type: 1,
    subscribe:1,
    showPage:false, //是否显示页面
    confirmButton:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var goodsList = JSON.parse(options.goodsList);
    this.setData({
      uid:options.uid,
      goodsList: goodsList
    })
  },

  onShow:function(){
    wx.showLoading({
      title: '加载中...',
    })
    this.subscribe();
    this.setData({
      showPage:false,
      confirmButton: true
    })
  },

  back: function() {
    wx.navigateBack({
      delta: 1
    })
  },

  confirm: function() {
    var that = this;

    if (this.data.confirmButton==false){
      return;
    }

    this.setData({
      confirmButton:false
    })
    var params = new Object()
    var list = [];
    params.record = [];
    for (var i = 0; i < this.data.goodsList.length; i++) {
      var box = {};
      box.sku_code = this.data.goodsList[i].sku_code
      box.num = this.data.goodsList[i].selected_num
      list.push(box);
    }

    params.record = JSON.stringify(list);
    params.uid=that.data.uid
    api.POST({
      params: params,
      url: app.globalData.base_url + 'begwine/apply',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          wx.setStorageSync('begSuccess', 'success'); //缓存讨酒是否成功
          wx.redirectTo({
            url: '/play/pages/begSuccessful/begSuccessful',
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail() {
        util.showError("网络错误");
      }
    })
  },

  //是否关注
  subscribe:function(){
    var that = this;
    var params = new Object()

    params.uid = that.data.uid
    api.POST({
      params: params,
      url: app.globalData.base_url + 'askWine/subscribe',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            showPage:true,
            subscribe: res.data.data.subscribe 
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail() {
        wx.hideLoading();
        util.showError("网络错误");
      }
    })

  }
})